﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Blog.Net.Business.BusinessObjects;
using Blog.Net.DataAccess.DataControls;

namespace Blog.Net.Blog
{
    public partial class Default : System.Web.UI.Page
    {
        /// <summary>
        /// Gets or sets the current page.
        /// </summary>
        /// <value>The current page.</value>
        public int CurrentPage
        {
            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 0;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }

            set
            {
                this.ViewState["CurrentPage"] = value;
            }
        }

        PagedDataSource entriesSource = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                dataBind();
        }

        private void dataBind()
        {
            IList<BlogEntryBO> blogs;
            //blogs = BlogEntryDataControl.Instance.GetLatest(-1);
            string filter = string.IsNullOrEmpty(Request.QueryString["Filter"]) ? null : Request.QueryString["Filter"];
            int entry = string.IsNullOrEmpty(Request.QueryString["EntryId"]) ? -1:int.Parse(Request.QueryString["EntryId"]);
            
            blogs = BlogEntryDataControl.Instance.GetLatest(-1,entry,filter);
            entriesSource.DataSource = blogs;
            entriesSource.AllowPaging = true;
            entriesSource.PageSize = 5;
            entriesSource.CurrentPageIndex = CurrentPage;
            blogEntries.DataSource = entriesSource;
            blogEntries.DataBind();
        }

        protected void Previous_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;           
            dataBind();
            if (entriesSource.IsFirstPage)
                Previous.Visible = false;
            if (!entriesSource.IsLastPage)
                Next.Visible = true;
        }

        protected void Next_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            dataBind();
            if (!entriesSource.IsFirstPage)
                Previous.Visible = true;
            if (entriesSource.IsLastPage)
                Next.Visible = false;
        }
    }
}
